package com.the4man.framework.action;

import com.the4man.framework.util.DateUtil;
import com.the4man.framework.util.LOG;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.convention.annotation.Action;

import javax.imageio.ImageIO;
import javax.imageio.stream.MemoryCacheImageInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

/**
 * Created by lenovo on 2017/12/12.
 */
@Action("fileUploadAction")
public class FileUploadAction extends BaseAction {
    private static final long serialVersionUID = 1L;
    private File filedata;
    private String filedataFileName;
    private String filedataContentType;

    public FileUploadAction() {
    }

    public void uploadFile() throws Exception {
        String realpath = this.getClass().getClassLoader().getResource("/").getPath();
        int endIndex = realpath.indexOf("/WEB-INF");
        realpath = realpath.substring(0, endIndex);
        realpath = realpath + "/upload";
        int pos = this.filedataFileName.lastIndexOf(".");
        String fileExt = this.filedataFileName.substring(pos + 1);
        realpath = realpath + "/" + fileExt;
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + fileExt;
        String ymd = "/" + DateUtil.getInstance().getYMDTimeStamp() + "/";
        realpath = realpath + ymd;
        File uploadDir = new File(realpath);
        if(!uploadDir.exists()) {
            uploadDir.mkdirs();
        }

        String err = "";
        if(this.filedata != null) {
            File savefile = new File(new File(realpath), newFileName);
            if(!savefile.getParentFile().exists()) {
                savefile.getParentFile().mkdirs();
            }

            FileInputStream fis = new FileInputStream(this.filedata);
            int fileLength = fis.available();
            Iterator itr = ImageIO.getImageReaders(new MemoryCacheImageInputStream(fis));
            if(fileLength > 307200 && itr.hasNext()) {
                try {
                    Thumbnails.of(new File[]{this.filedata}).scale(0.25D).toFile(savefile);
                } catch (Exception var14) {
                    FileUtils.copyFile(this.filedata, savefile);
                    LOG.error("压缩图片错误，直接拷贝源文件.", var14, new String[0]);
                }
            } else {
                FileUtils.copyFile(this.filedata, savefile);
            }
        } else {
            err = "upload  failure !";
        }

        Map<String, String> map = new HashMap();
        map.put("err", err);
        map.put("msg", "/upload/" + fileExt + ymd + newFileName);
        this.writeJson(map);
    }

    public File getFiledata() {
        return this.filedata;
    }

    public void setFiledata(File filedata) {
        this.filedata = filedata;
    }

    public String getFiledataFileName() {
        return this.filedataFileName;
    }

    public void setFiledataFileName(String filedataFileName) {
        this.filedataFileName = filedataFileName;
    }

    public String getFiledataContentType() {
        return this.filedataContentType;
    }

    public void setFiledataContentType(String filedataContentType) {
        this.filedataContentType = filedataContentType;
    }
}
